package outoforder.leetcode02;

import org.junit.Test;

import java.util.Arrays;

/**
 * @author shhjiang_001
 * @create 2020-07-18 21:12
 */
// 优势洗牌，田忌赛马
public class Solution870 {
    public int[] advantageCount(int[] A, int[] B) {
        int[][] ints = new int[B.length][2];
        for (int i = 0; i < B.length; i++) {
            ints[i][0] = B[i];
            ints[i][1] = i;
        }
        Arrays.sort(ints,(a,b)->a[0]-b[0]);
        Arrays.sort(A);
        int[] res = new int[A.length];
        int b = 0;
        int r = A.length-1;
        for (int i = 0; i < A.length; i++) {
            if (A[i] > ints[b][0]){
                res[ints[b++][1]] = A[i];
            }else {
                res[ints[r--][1]] = A[i];
            }
        }
        return res;
    }
    @Test
    public void test(){
        advantageCount(new int[]{12,24,8,32},new int[]{13,25,32,11});
    }
}
